package searchUser;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import deleteUser.DeleteUser;
import libraryRun.MainFrame;
import mySQLServer.Link;

public class SearchUser extends JFrame {
    private int sum=0;
    private SearchUser mySearchUser;
    public SearchUser(){
        super("用户查询界面");
        mySearchUser=this;
    }

    public void search(MainFrame myJFrame){
        setLayout(null);
        Container container=this.getContentPane();
        setSize(1200,650);

        //面板
        JPanel p1=new JPanel(new FlowLayout(FlowLayout.CENTER,0,35));
        JPanel p2=new JPanel(new FlowLayout(FlowLayout.CENTER,10,35));
        JPanel p3=new JPanel(new BorderLayout());

        //标签
        JLabel label1=new JLabel("用 户 查 询");
        JLabel label2=new JLabel("用 户 名");
        JLabel label3=new JLabel("用 户 Id");

        //文本行
        JTextField field1=new JTextField(15);
        JTextField field2=new JTextField(15);

        //按钮
        JButton button1=new JButton("查 询");
        JButton button2=new JButton("取 消");

        //表格
        String[] tableHeader={"用户名","用户Id","用户加密密码"};
        Object[][] tableData=new Object[50][3];
        JTable jTable=new JTable(tableData,tableHeader);

        //滚动框
        JScrollPane jScrollPane=new JScrollPane(jTable);

        //颜色
        Color color1=new Color(203, 26, 40);
        Color color2=new Color(87, 78, 64, 116);
        Color color3=new Color(7, 124, 161, 255);
        Color color4=new Color(13, 50, 197,225);

        //字体
        Font font1=new Font("宋体",Font.BOLD,45);
        Font font2=new Font("宋体",Font.BOLD,30);
        Font font3=new Font("宋体",Font.BOLD,17);
        Font font4=new Font("宋体",Font.BOLD,15);
        Font font5=new Font("宋体",Font.BOLD,14);

        //设置字体
        label1.setFont(font1);
        label2.setFont(font2);
        label3.setFont(font2);
        field1.setFont(font3);
        field2.setFont(font3);

        //设置大小
        label2.setPreferredSize(new Dimension(150,30));
        label3.setPreferredSize(new Dimension(150,30));
        button1.setPreferredSize(new Dimension(100,30));
        button2.setPreferredSize(new Dimension(100,30));

        //设置位置
        p1.setBounds(220,30,750,150);
        p2.setBounds(200,200,320,300);
        p3.setBounds(600,220,400,200);

        //表头设置
        jTable.getTableHeader().setResizingAllowed(false); //用户不可以通过拖拉表头间调整各列大小
        jTable.getTableHeader().setReorderingAllowed(false); //用户不可以通过拖动表头以重新排列各列
        jTable.getTableHeader().setFont(font4);
        jTable.getTableHeader().setForeground(color1);

        //表格设置
        jTable.setRowHeight(20); //设置行高
        jTable.setFont(font5);
        jTable.setForeground(color4);
        jTable.setSelectionForeground(color3); //设置被选中的行前景(字体颜色)
        jTable.setSelectionBackground(color2); //设置被选中的行背景


        //放入面板
        p1.add(label1);

        p2.add(label2);
        p2.add(field1);
        p2.add(label3);
        p2.add(field2);
        p2.add(button1);
        p2.add(button2);

        p3.add(jScrollPane);

        //注册监听器
        ImageIcon imageIcon1=new ImageIcon("icon/imageIcon6.jpg");
        imageIcon1.setImage(imageIcon1.getImage().getScaledInstance(50,45,Image.SCALE_AREA_AVERAGING));
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cleanTable(jTable);
                String sql=null;
                Link link=new Link();
                try{
                    Connection con=link.linkDatebase();
                    if(!"".equals(field1.getText())){
                        if(!"".equals(field2.getText())){
                            sql="select * from [user] where userName='"+field1.getText()+"' and userId='"+field2.getText()+"'";
                            searchIntoJTable(con,jTable,sql,imageIcon1);//bookName、bookId、author不为空
                        }
                        else{
                            sql="select * from [user] where userName="+"'"+field1.getText()+"'";
                            searchIntoJTable(con,jTable,sql,imageIcon1);
                        }
                    }
                    else{
                        if(!"".equals(field2.getText())){
                            sql="select * from [user] where userId='"+field2.getText()+"'";
                            searchIntoJTable(con,jTable,sql,imageIcon1);
                        }
                        else{
                            JOptionPane.showMessageDialog(mySearchUser,"请输入完整用户信息","输入确认",1,imageIcon1);
                        }
                    }
                    link.closeDatebase(con);
                }catch (ClassNotFoundException e1){
                    e1.printStackTrace();
                }catch (SQLException e1){
                    e1.printStackTrace();
                }
            }
        });

        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                myJFrame.setVisible(true);
                dispose();
            }
        });

        mySearchUser.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                myJFrame.setVisible(true);
            }
        });

        //放入容器
        container.add(p1);
        container.add(p2);
        container.add(p3);

        setIconImage((new ImageIcon("icon/imageIcon5.jpg")).getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }

    public void searchIntoJTable (Connection con,JTable jTable,String sql,ImageIcon imageIcon1) throws SQLException{
        int flag=0;
        sum=0;
        Statement stmt=con.createStatement();
        ResultSet rs=stmt.executeQuery(sql);
        while (rs.next()){
            flag=1;
            jTable.getModel().setValueAt(rs.getString("userName"),sum,0);
            jTable.getModel().setValueAt(rs.getString("userId"),sum,1);
            int lockPassword=rs.getString("userPassword").hashCode();
            if(lockPassword<0)
                lockPassword=-lockPassword;
            jTable.getModel().setValueAt(lockPassword,sum,2);
            sum++;
        }
        if (flag==0){
            JOptionPane.showMessageDialog(mySearchUser,"未查询到用户信息","确认查询",1,imageIcon1);
        }
        else{
            JOptionPane.showMessageDialog(mySearchUser,"查询成功","确认查询",1,imageIcon1);
        }
    }

    public void cleanTable(JTable jTable){
        for(int i=0;i<sum;i++){
            jTable.getModel().setValueAt("",i,0);
            jTable.getModel().setValueAt("",i,1);
            jTable.getModel().setValueAt("",i,2);

        }
    }

}
